package com.ruoyi.web.controller.bus.controller;

import com.ruoyi.bus.domain.BusReportMinute;
import com.ruoyi.bus.dto.BusDataDayResult;
import com.ruoyi.bus.dto.BusDataParam;
import com.ruoyi.bus.service.IBusReportMinuteService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Method;
import java.util.*;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2021-09-01
 */
@Controller
@RequestMapping("/bus/minute")
public class BusReportMinuteController extends BaseController
{
    private String prefix = "bus/minute";

    @Autowired
    private IBusReportMinuteService busReportMinuteService;

    @RequiresPermissions("bus:minute:view")
    @GetMapping()
    public String minute()
    {
        return prefix + "/minute";
    }


    @RequiresPermissions("bus:minute:dayTrend")
    @GetMapping("/dayTrend")
    public String trend1()
    {
        return prefix + "/dataTrend";
    }

    @RequiresPermissions("bus:minute:monthTrend")
    @GetMapping("/monthTrend")
    public String trend2()
    {
        return prefix + "/monthTrend";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("bus:minute:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BusReportMinute busReportMinute)
    {
        startPage();
        List<BusReportMinute> list = busReportMinuteService.selectBusReportMinuteList(busReportMinute);
        return getDataTable(list);

    }
    //月趋势横坐标
    @RequiresPermissions("bus:minute:dataTrend")
    @PostMapping("xAxis1")
    @ResponseBody
    public List xAxis1(Model model,@RequestBody BusDataParam busDataParam){
        List<String> xAxis = new ArrayList<String>();
        List<String> list = busReportMinuteService.selectMonth(busDataParam);
        if (!CollectionUtils.isEmpty(list)) {
            for (int i=0;i<list.size();i++) {
                if (i%2 == 0) {
                    xAxis.add(list.get(i));
                }
            }
        }
        model.addAttribute("xAxis", xAxis);
        return xAxis;
    }
    //月趋势对应的值
    @RequiresPermissions("bus:minute:dataTrend")
    @PostMapping("data1")
    @ResponseBody
    public List data1(Model model,@RequestBody BusDataParam busDataParam){
        List<String> xAxis = new ArrayList<>();
        List<Object> data = new ArrayList<>();
        String str = "化学需氧量,氨氮,总磷,总氮,PH,电导,溶氧,浊度,温度";
        String strCode = "Cod,Ad,Zl,Tn,Ph,Cond,Ry,Zd,Wd";
        List<String> propertyName = Arrays.asList(str.split(","));
        List<String> codeName = Arrays.asList(strCode.split(","));
        List<String> xlist = busReportMinuteService.selectMonth(busDataParam);
        if (!CollectionUtils.isEmpty(xlist)) {
            for (int i=0;i<xlist.size();i++) {
                if (i%2 == 0) {
                    xAxis.add(xlist.get(i));
                }
            }
        } else {
            model.addAttribute("propertyName", propertyName);
            model.addAttribute("xAxis", xAxis);
            model.addAttribute("data", data);
            return data;
        }
        List<BusDataDayResult> list = busReportMinuteService.selectMonthData(busDataParam);
        for(int i=0;i<codeName.size();i++){
            List<Double> k = new ArrayList<>();
            for (BusDataDayResult obj : list) {
                String getMethodName = "get" + codeName.get(i);
                String val = null;
                try {
                    Method m = obj.getClass().getMethod(getMethodName);
                    val = (String) m.invoke(obj,null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                k.add(Double.valueOf(StringUtils.isBlank(val)?"0":val));
            }
            Map<String,Object> item = new HashMap<>();
            item.put("name", propertyName.get(i));
            item.put("type", "line");
            item.put("data", k);
            data.add(item);
        }
        model.addAttribute("propertyName", propertyName);
        model.addAttribute("xAxis", xAxis);
        model.addAttribute("data", data);
        return data;
    }



    //日趋势横坐标
    @RequiresPermissions("bus:minute:dataTrend")
    @PostMapping("xAxis")
    @ResponseBody
    public List xAxis(){
        List<String> xAxis = new ArrayList<String>();
        //模拟生成x轴的12个小时
        for(int i = 0; i < 6; i++){
            xAxis.add(i*4 +":00:00" );
        }
        return xAxis;
    }
    //日趋势对应的值
    @RequiresPermissions("bus:minute:dataTrend")
    @PostMapping("data")
    @ResponseBody
    public List data(Model model,@RequestBody BusDataParam busDataParam){
        List<BusDataDayResult> list = busReportMinuteService.selectBusDataTrend(busDataParam);
        if (!CollectionUtils.isEmpty(list) && !"00".equals(list.get(0).gethTime())) {
            BusDataDayResult entity = new BusDataDayResult();
            entity.sethTime("00");
            list.add(0,entity);
        }
        if (!CollectionUtils.isEmpty(list) && list.size() < 6) {
            for (int i=0;i<(6-list.size());i++) {
                BusDataDayResult entity = new BusDataDayResult();
                list.add(entity);
            }
        }
        String str = "化学需氧量,氨氮,总磷,总氮,PH,电导,溶氧,浊度,温度";
        String strCode = "Cod,Ad,Zl,Tn,Ph,Cond,Ry,Zd,Wd";
        List<String> propertyName = Arrays.asList(str.split(","));
        List<String> codeName = Arrays.asList(strCode.split(","));
        List<String> xAxis = new ArrayList<String>();
        //模拟生成x轴的12个小时
        for(int i = 0; i < 6; i++){
            xAxis.add(i*4 +":00:00" );
        }
        List<Object> data = new ArrayList<>();
        for(int i=0;i<codeName.size();i++){
            List<Double> k = new ArrayList<>();
            for (BusDataDayResult obj : list) {
                String getMethodName = "get" + codeName.get(i);
                String val = null;
                try {
                    Method m = obj.getClass().getMethod(getMethodName);
                    val = (String) m.invoke(obj,null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                k.add(Double.valueOf(StringUtils.isBlank(val)?"0":val));
            }
            Map<String,Object> item = new HashMap<>();
            item.put("name", propertyName.get(i));
            item.put("type", "line");
            item.put("data", k);
            data.add(item);
        }
        model.addAttribute("propertyName", propertyName);
        model.addAttribute("xAxis", xAxis);
        model.addAttribute("data", data);
        return data;
    }
    //所有指标属性
    @RequiresPermissions("bus:minute:dataTrend")
    @PostMapping("property")
    @ResponseBody
    public List trend(Model model){
        List<String> propertyName = new ArrayList<String>();
        //模拟生成所有指标
        propertyName.add("氨氮");
        propertyName.add("总磷");
        propertyName.add("浊度");
        propertyName.add("温度");
        propertyName.add("叶绿素");
        model.addAttribute("propertyName", propertyName);
        return propertyName;
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("bus:minute:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BusReportMinute busReportMinute)
    {
        List<BusReportMinute> list = busReportMinuteService.selectBusReportMinuteList(busReportMinute);
        ExcelUtil<BusReportMinute> util = new ExcelUtil<BusReportMinute>(BusReportMinute.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("bus:minute:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BusReportMinute busReportMinute)
    {
        return toAjax(busReportMinuteService.insertBusReportMinute(busReportMinute));
    }

    /**
     * 修改【请填写功能名称】
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        BusReportMinute busReportMinute = busReportMinuteService.selectBusReportMinuteById(id);
        mmap.put("busReportMinute", busReportMinute);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("bus:minute:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BusReportMinute busReportMinute)
    {
        return toAjax(busReportMinuteService.updateBusReportMinute(busReportMinute));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("bus:minute:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(busReportMinuteService.deleteBusReportMinuteByIds(ids));
    }

}
